package com.yeqian.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yeqian.vo.PageVO;
import com.yeqian.article.service.CommentService;
import com.yeqian.article.entity.Comment;
import com.yeqian.article.mapper.CommentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 评论 服务
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    /**
     * 获取评论树
     *
     * @param pageVO
     * @return
     */
    @Override
    public PageInfo<Map<String, Object>> treeComment(PageVO pageVO) {
        PageHelper.startPage(pageVO.getPageNum(), pageVO.getPageSize());
        return PageInfo.of(listToTree("-1", commentMapper.listComment(pageVO)));
    }

    private List<Map<String, Object>> listToTree(String pid, List<Map<String, Object>> comments) {
        List<Map<String, Object>> result = new ArrayList<>();
        Optional.ofNullable(comments)
                /**
                 * 为空的话就初始化一个空的list
                 */
                .orElse(new ArrayList<>())
                /**
                 * 不为空的话就开启stream流
                 */
                .stream()
                /**
                 *  第一次筛选出主父节点列表进入循环，循环里面 进入递归 筛选出递归传递的从父节点列表
                 *  筛选出主父节点对象（第一层数据）
                 *  Optional.ofNullable(root)
                 */
                .filter(root -> pid.equals(root.get("replyId") == null ? "" : root.get("replyId").toString()))
                /**
                 * 递归，最末的父节点从整个列表筛选出它的子节点列表依次组装
                 * 依次从整个列表中帅选出对应的子节点的数据，并挂载到对应的父节点上，最终将父节点存入集合
                 */
                .forEach(commentVO -> {
                    String id = commentVO.get("id").toString();
                    Map<String, Object> map = new HashMap<>();
                    List<Map<String, Object>> children = listToTree(id, comments);
                    map.put("id", id);
                    map.put("content", commentVO.get("commentContent"));
                    map.put("contentUserName", commentVO.get("contentUserName"));
                    map.put("contentUserId", commentVO.get("contentUserId"));
                    map.put("createTime", commentVO.get("createTime"));
                    map.put("children", children);
                    result.add(map);
                });
        return result;
    }

    /**
     * 新增评论
     *
     * @param comment
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveComment(Comment comment) {
        commentMapper.insert(comment);
    }

    /**
     * 删除评论
     *
     * @param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteComment(String ids) {
        String[] idList = ids.split(",");
        for (String id : idList)   commentMapper.deleteById(id);
    }
}
